package com.talent.jiwen_teacher.my;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.liveaa.livemeeting.sdk.biz.core.ABCLiveSDK;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.talent.jiwen_teacher.CertificationInfoActivity;
import com.talent.jiwen_teacher.R;
import com.talent.jiwen_teacher.base.BaseFragment;
import com.talent.jiwen_teacher.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen_teacher.http.common.ApiFactory;
import com.talent.jiwen_teacher.http.common.HttpUtil;
import com.talent.jiwen_teacher.http.common.ProgressSubscriber;
import com.talent.jiwen_teacher.http.result.TeacherInfo;
import com.talent.jiwen_teacher.http.result.TeacherInfoResult;
import com.talent.jiwen_teacher.util.Constant;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.SPConstant;
import com.talent.jiwen_teacher.util.SPUtil;
import com.talent.jiwen_teacher.util.Validators;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

import static com.talent.jiwen_teacher.CertificationInfoActivity.EDIT_STYLE;
import static com.talent.jiwen_teacher.CertificationInfoActivity.TEACHER_IDENTITY;
import static com.talent.jiwen_teacher.my.WebShowActivity.WEB_CAO_TYPE;
import static com.talent.jiwen_teacher.my.WebShowActivity.WEB_CERT_TYPE;
import static com.talent.jiwen_teacher.my.WebShowActivity.WEB_INVISIT_TYPE;

public class MyFragment extends BaseFragment {
    @BindView(R.id.setIv)
    TextView setIv;
    @BindView(R.id.headIv)
    ImageView headIv;
    @BindView(R.id.nameTv)
    TextView nameTv;
    @BindView(R.id.subjectTv)
    TextView subjectTv;
    @BindView(R.id.codeTv)
    TextView codeTv;
    @BindView(R.id.orderLayout)
    RelativeLayout orderLayout;
    @BindView(R.id.evaluateLayout)
    RelativeLayout evaluateLayout;
    @BindView(R.id.teachTimeLayout)
    RelativeLayout teachTimeLayout;
    @BindView(R.id.helpCenterLayout)
    RelativeLayout helpCenterLayout;
    @BindView(R.id.aboutLayout)
    RelativeLayout aboutLayout;

    @BindView(R.id.invisit_activity)
    ImageView invisit_activity;
    @BindView(R.id.ll_take)
    RelativeLayout ll_take;

    @BindView(R.id.user_status)
    TextView user_status;//认证状态

    @BindView(R.id.fengLayout)
    RelativeLayout fengLayout;//风采展示

    @BindView(R.id.renZhengLayout)
    RelativeLayout renZhengLayout;//认证中心

    @BindView(R.id.moneyTv)
    TextView moneyTv;

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    @BindView(R.id.img_teyao)
    ImageView img_teyao;//特邀徽章

    @Override
    protected void initData() {
        registEventBus();
        if (Validators.isEmpty(SPUtil.getUserName())) {
            nameTv.setText(SPUtil.getUserPhone());
        } else {
            nameTv.setText(SPUtil.getUserName());
        }
        codeTv.setText(getString(R.string.invite_code) + SPUtil.stringOut(SPConstant.INVITE_CODE));
        ImageUtil.loadCircleImage(getContext(), SPUtil.getUserHeadImg(), headIv);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getInfo();
            }
        });
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.autoRefresh();

    }

    public static final String REFRESH_DATA = "refresh";

    @Subscribe
    public void onEvent(String flag){
        if (flag.equals(REFRESH_DATA)){
            getInfo();
        }
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_my;
    }

    @OnClick({R.id.orderLayout, R.id.evaluateLayout, R.id.teachTimeLayout, R.id.helpCenterLayout, R.id.aboutLayout, R.id.setIv, R.id.invisit_activity, R.id.ll_take, R.id.fengLayout, R.id.renZhengLayout, R.id.infoLayout, R.id.fanLayout, R.id.xinLayout, R.id.rl_money_rc, R.id.caoLayout, R.id.ruleLayout,R.id.qrCodeLayout,R.id.moneyLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.orderLayout:
                intentActivity(OrderActivity.class);
                break;
            case R.id.evaluateLayout:
                intentActivity(MyEvaluateActivity.class);
                break;
            case R.id.teachTimeLayout:
                Intent intent1 = new Intent(getActivity(), TimeSetActivity.class);
                intent1.putExtra("teacherInfo", teacherInfo);
                startActivity(intent1);
                break;
            case R.id.helpCenterLayout:
                break;
            case R.id.aboutLayout:
                break;
            case R.id.setIv:
                intentActivity(SetActivity.class);
                break;
            case R.id.invisit_activity:
                WebShowActivity.WEB_SHOW_TYPE = WEB_INVISIT_TYPE;
                intentActivity(WebShowActivity.class);
                break;
            case R.id.ll_take:
                intentActivity(WithdrawActivity.class);
                break;
            case R.id.fengLayout://风采
                intentActivity(StyleShowActivity.class);
                break;
            case R.id.renZhengLayout://认证
                Intent intent = new Intent(getActivity(), CertificationInfoActivity.class);
                intent.putExtra(TEACHER_IDENTITY, SPUtil.intOut(SPConstant.TEACHER_IDENTITY));
                intent.putExtra(EDIT_STYLE, 1);
                startActivity(intent);
                break;
            case R.id.infoLayout:
                intentActivity(PersonInfoActivity.class);
                break;
            case R.id.fanLayout://定价中心
                intentActivity(PricingActivity.class);
                break;
            case R.id.xinLayout://信用中心
                intentActivity(CreditActivity.class);
                break;
            case R.id.rl_money_rc://流水记录
                intentActivity(RunningWaterActivity.class);
                break;
            case R.id.caoLayout:
                WebShowActivity.WEB_SHOW_TYPE = WEB_CAO_TYPE;
                intentActivity(WebShowActivity.class);
                break;
            case R.id.ruleLayout:
                WebShowActivity.WEB_SHOW_TYPE = WEB_CERT_TYPE;
                intentActivity(WebShowActivity.class);
                break;
            case R.id.qrCodeLayout:
                intentActivity(MyQRCodeActivity.class);
                break;
            case R.id.moneyLayout://如何邀请赚钱
                WebShowActivity.WEB_SHOW_TYPE = WebShowActivity.WEB_INVISIT_MONEY;
                intentActivity(WebShowActivity.class);
                break;
        }
    }


    TeacherInfo teacherInfo;

    private void getInfo() {
        Map<String, String> map = new HashMap<>();
        map.put("teacherId", SPUtil.getUserId());
        Observable observable = ApiFactory.getEducationApiSingleton().getTeacherSelfInfo(SPUtil.getToken(), map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<TeacherInfoResult>(getActivity()) {
            @Override
            protected void _onNext(TeacherInfoResult teacherInfoResult) {
                refreshLayout.finishRefresh();
                teacherInfo=teacherInfoResult.getTeacherInfo();
                if(teacherInfo!=null){
                    int userStatus=teacherInfo.getUserStatus();//用户状态（1：正常 2：认证中 3：未填写个人信息 4：认证失败）
                    String state=null;
                    switch (userStatus){
                        case 1:
                            state="认证成功";
                            break;
                        case 2:
                            state = "认证中";
                            break;
                        case 3:
                            state = "请补全信息";
                            break;
                        case 4:
                            state = "认证失败,请重新提交";
                            break;
                    }
                    user_status.setText(state);

                    if(teacherInfo.getSpecialTeacher()==1){//特邀教师
                        img_teyao.setVisibility(View.VISIBLE);
                    }else{
                        img_teyao.setVisibility(View.GONE);
                    }
                    SPUtil.stringIn(SPConstant.NIM_TOKEN, teacherInfo.getNIMToken());
                    ABCLiveSDK.getInstance(getActivity()).initToken(SPUtil.getBSToken());
                    SPUtil.stringIn(SPConstant.USER_NAME, teacherInfo.getTeacherName());
                    SPUtil.stringIn(SPConstant.USER_HEAD_URL, teacherInfo.getTeacherHeadImage());
                    SPUtil.stringIn(SPConstant.INVITE_CODE, teacherInfo.getInviteCode());
                    SPUtil.stringIn(SPConstant.TEACH_YEARS, "" + teacherInfo.getTeachAge());
                    SPUtil.intIn(SPConstant.TEACHER_IDENTITY, teacherInfo.getTeacherType());
                    SPUtil.intIn(SPConstant.TEACH_PART, teacherInfo.getGradePart());
                    SPUtil.intIn(SPConstant.SUBJECT_ID, teacherInfo.getSubjectId());
                    SPUtil.stringIn(SPConstant.GRADUATE_SCHOOL_NAME, teacherInfo.getGraduatedSchool());
                    SPUtil.stringIn(SPConstant.VIDEO_URL, teacherInfo.getVideoUrl());
                    codeTv.setText(getString(R.string.invite_code) + teacherInfo.getInviteCode());
                    ImageUtil.loadCircleImage(getContext(), teacherInfo.getTeacherHeadImage(), headIv);
                    if (teacherInfo.getGradePart() != 0 && teacherInfo.getSubjectId() != 0) {
                        subjectTv.setText(Constant.partList[SPUtil.intOut(SPConstant.TEACH_PART) - 1] + Constant.subList[SPUtil.intOut(SPConstant.SUBJECT_ID) - 1]);
                    }

                    if (Validators.isEmpty(SPUtil.getUserName())) {
                        nameTv.setText(SPUtil.getUserPhone());
                    } else {
                        nameTv.setText(SPUtil.getUserName());
                    }

                    if (Validators.isEmpty(teacherInfo.getTotalMoney())) {
                        moneyTv.setText("0.0");
                    } else {
                        moneyTv.setText(teacherInfo.getTotalMoney());
                    }

                }

            }

            @Override
            protected void _onError(String message) {
                if (refreshLayout != null) {
                    refreshLayout.finishRefresh();
                }
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject, false);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegistEventBus();
    }
}
